package vip.specimen.system.controller.resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemResourceKindEntity;
import vip.specimen.system.datasource.validation.SystemResourceKindValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemResourceKindService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/10 13:08
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 * 资源分类管理
 **/
@Api(tags = "资源分类")
@RestController
@RequestMapping(value = "/resource/kind")
public class KindController {
    @Resource
    SystemResourceKindService resourceKindService;

    @PutMapping
    @RequiresPermissions(value = "resource:kind:insert")
    @ApiOperation(value = "添加", notes = "添加资源分类")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加资源分类")
    public JsonResult<SystemResourceKindEntity> insert(@RequestBody @Valid SystemResourceKindValidation kindValidation) {
        return JsonResult.success(this.resourceKindService.insert(kindValidation));
    }

    @GetMapping
    @RequiresPermissions(value = "resource:kind:select")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看资源分类")
    @ApiOperation(value = "查看", notes = "获取资源分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemResourceKindEntity>> findAll() {
        return JsonResult.success(this.resourceKindService.findAll());
    }


    @GetMapping(value = "search")
    @RequiresPermissions(value = "resource:kind:search")
    @SystemJurisdiction(resourceType = "button", permissionName = "搜索资源分类")
    @ApiOperation(value = "搜索", notes = "根据关键词搜索资源分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult findByNameAll(@RequestParam(value = "name", required = false, defaultValue = "") String name) {
        return JsonResult.success(this.resourceKindService.findSystemResourceKindEntityByNameLike(name));
    }

    @DeleteMapping
    @RequiresPermissions(value = "resource:kind:delete")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除资源分类")
    @ApiOperation(value = "删除", notes = "根据uuid删除资源分类")
    public JsonResult delete(@RequestBody HashMap<String, UUID> param) {
        return this.resourceKindService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }

    @PostMapping(value = "enable")
    @RequiresPermissions(value = "resource:kind:update:enable")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新资源分类状态")
    @ApiOperation(value = "更新状态", notes = "根据uuid更新资源分类状态")
    public JsonResult<SystemResourceKindEntity> update(@RequestBody HashMap<String, UUID> param) {
        return JsonResult.success(this.resourceKindService.update(param.get("uuid")));
    }


    @PostMapping()
    @RequiresPermissions(value = "resource:kind:update")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新资源分类")
    @ApiOperation(value = "更新", notes = "根据uuid更新资源分类")
    public JsonResult<SystemResourceKindEntity> update(@RequestParam("uuid") @NotNull(message = "参数不可为空") UUID uuid,  @RequestBody @Valid SystemResourceKindValidation kindValidation) {
        return JsonResult.success(this.resourceKindService.update(uuid, kindValidation));
    }


    @DeleteMapping(value = "batch")
    @RequiresPermissions(value = "resource:kind:delete:batch")
    @SystemJurisdiction(resourceType = "button", permissionName = "批量删除资源分类")
    @ApiOperation(value = "批量删除", notes = "根据uuid批量删除资源分类")
    public JsonResult<HashMap<String, Integer>> deletes(@RequestBody HashMap<String, Set<UUID>> param) {
        int result = this.resourceKindService.delete(param.get("uuids"));
        return JsonResult.success(new HashMap<String, Integer>(1) {{
            put("successNum", result);
        }});
    }
}
